package com.jwthhealth_pub.wristband.steps.db;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.dao.Dao;
import com.jwthhealth_pub.app.App;
import com.jwthhealth_pub.old.signutil.db.DbHelper;
import com.jwthhealth_pub.wristband.steps.bean.StepsTimeModel;

import java.sql.SQLException;
import java.util.List;

/**
 * 每天24小时的运动
 * Created by yang on 2017/12/25.
 */

public class StepsTimeDao {
    private static StepsTimeDao instance;
    private Context context;
    private Dao<StepsTimeModel, Integer> stepsDayDao;
    private DbHelper databaseHelper;
    private StepsTimeModel dataBean;

    @SuppressWarnings("unchecked")
    public StepsTimeDao(Context context) {
        super();
        this.context = context;

        try {
            databaseHelper = DbHelper.getHelper(context);
            stepsDayDao = databaseHelper.getDao(StepsTimeModel.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public synchronized static StepsTimeDao getInstance() {

        if (instance == null) {
            instance = new StepsTimeDao(App.mContext);
        }

        return instance;
    }

    /**
     * 插入一条数据
     * @param
     * @return
     */
    public boolean add(StepsTimeModel stepsDayModel){
        boolean isInsert = false;

        try {
            int result = stepsDayDao.create(stepsDayModel);
            if (result > 0)
                isInsert = true;
        } catch (SQLException e) {
            Log.e("StepsDayDao", e.toString());
        }
        return isInsert;
    }
    /*
    * 根据日期查询StepsDayModel
    */
    public StepsTimeModel queryByDateStepsDayModel(String day_date) {
        try {
            List<StepsTimeModel> dataBeens = stepsDayDao.queryForEq("day_date",day_date);
            if (dataBeens.size() > 0) {
                dataBean = dataBeens.get(0);
            } else {
                dataBean = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }


        return dataBean;
    }

    /*
     * 根据日期查询有没有相同的  false没有 true有  在执行add()
     */
    public boolean  queryByDateEqual(String day_date) {
        Boolean isEqual =false;
        try {
            List<StepsTimeModel> dataBeens = stepsDayDao.queryForEq("day_date",day_date);
            if (dataBeens.size() > 0) {
                isEqual =true;
            } else {
                isEqual =false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return isEqual;
    }
}
